package com.assignment.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.BevelBorder;
import java.awt.Label;
import java.awt.Font;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JButton;

import com.assignment.database.DBOperations;
import com.assignment.main.Supplier;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class EditSupplierScreen extends JFrame {

	private JPanel contentPane;
	private JTextField nameText;
	private JTextField collectionText;
	private JTextArea addressText;

	Supplier supplier;
	JFrame thisFrame = this;
	final SearchScreen screen;
	
	public EditSupplierScreen(Supplier supplier,SearchScreen screen) {
		this(screen);
		
		this.supplier=supplier;
		
		
		addressText.setText(supplier.getAddress());
		collectionText.setText(supplier.getCollection());
		nameText.setText(supplier.getName());
		
	}
	/**
	 * @wbp.parser.constructor
	 */
	public EditSupplierScreen(final SearchScreen screen) {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 568, 400);
		this.screen=screen;
		setLocationRelativeTo(null);
		setVisible(true);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel.setBounds(10, 49, 532, 154);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("Supplier Name:");
		lblNewLabel.setBounds(21, 11, 99, 23);
		panel.add(lblNewLabel);
		
		nameText = new JTextField();
		nameText.setBounds(130, 12, 86, 20);
		panel.add(nameText);
		nameText.setColumns(10);
		
		JLabel lblCollection = new JLabel("Collection:");
		lblCollection.setBounds(21, 65, 99, 14);
		panel.add(lblCollection);
		
		collectionText = new JTextField();
		collectionText.setBounds(130, 62, 86, 20);
		panel.add(collectionText);
		collectionText.setColumns(10);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(264, 15, 72, 14);
		panel.add(lblAddress);
		
		addressText = new JTextArea();
		addressText.setBounds(346, 10, 176, 93);
		panel.add(addressText);
		addressText.setColumns(10);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_1.setBounds(227, 0, 107, 38);
		contentPane.add(panel_1);
		
		Label label = new Label("Edit Supplier");
		label.setFont(new Font("Dialog", Font.BOLD, 14));
		panel_1.add(label);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
		panel_2.setBounds(10, 229, 532, 110);
		contentPane.add(panel_2);
		panel_2.setLayout(null);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				
				String[] data = new String[4];
				
				if (supplier!=null) {
					data[0] = supplier.getId();
					}
				data[1] = nameText.getText();
				data[2] = collectionText.getText();
				data[3] = addressText.getText();
				
				Supplier sup = new Supplier(data);
				
				DBOperations.saveSupplier(sup, data[0]==null ? true : false);
				screen.refreshList();
				thisFrame.dispose();
			}
		});
		btnSave.setBounds(133, 37, 89, 23);
		panel_2.add(btnSave);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnCancel.setBounds(330, 37, 89, 23);
		panel_2.add(btnCancel);
	}
}
